package HandleXml.service;

import HandleXml.model.Book;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.List;

import HandleXml.model.BookStore;
import org.junit.Test;

public class BookJaxbService {
    private static final String XML_FILE = "src/main/resources/books.xml";

    @Test
    public void testJaxbRead() {
        // 读取XML文件
        BookStore bookStore = readBookFromXml();
        System.out.println("\n=== 从XML读取的图书信息 ===");
        printBookInfo(bookStore);
    }

    @Test
    public void testJaxbWrite() {
        BookStore bookstore = readBookFromXml();
        List<Book> books = bookstore.getBooks();
        for (Book book1 : books) {
            Book.Price price = new Book.Price();
            price.setValue(99.99);
            price.setCurrency("CNY");
            book1.setPrice(price);
        }
        writeBookToXml(bookstore, "src/main/resources/book_updated.xml");
        System.out.println("\n=== XML文件已更新 ===");
    }

    // 使用JAXB从XML文件读取Book对象
    public static BookStore readBookFromXml() {
        try {
            JAXBContext context = JAXBContext.newInstance(BookStore.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (BookStore) unmarshaller.unmarshal(new File(XML_FILE));
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 使用JAXB将Book对象写入XML文件
    public static void writeBookToXml(BookStore bookStore, String filePath) {
        try {
            JAXBContext context = JAXBContext.newInstance(BookStore.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化输出
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 设置编码

            // 写入到文件
            marshaller.marshal(bookStore, new File(filePath));

//            // 同时打印到控制台
//            StringWriter writer = new StringWriter();
//            marshaller.marshal(bookStore, writer);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    // 打印图书信息
    private static void printBookInfo(BookStore bookStore) {
        if (bookStore == null) {
            System.out.println("未能成功读取图书信息");
            return;
        }
        List<Book> books = bookStore.getBooks();
        for (Book book : books) {
            System.out.println("ID: " + book.getId());
            System.out.println("类别: " + book.getCategory());
            System.out.println("标题: " + book.getTitle().getValue());
            System.out.println("语言: " + book.getTitle().getLang());
            System.out.println("作者: " + book.getAuthor().getName());
            System.out.println("国籍: " + book.getAuthor().getNationality());
            if (book.getAuthor().getEmail() != null) {
                System.out.println("邮箱: " + book.getAuthor().getEmail());
            }
            if (book.getPublisher() != null) {
                System.out.println("出版社: " + book.getPublisher().getName());
                System.out.println("地址: " + book.getPublisher().getAddress());
            }
            System.out.println(String.format("价格: %.2f %s", book.getPrice().getValue(), book.getPrice().getCurrency()));
            System.out.println("描述: " + book.getDescription());
            System.out.println("出版日期: " + book.getPubdate());
        }
    }
}